package com.tingyuge.thinking.spring.resource;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.EncodedResource;

import java.io.File;
import java.io.IOException;
import java.io.Reader;

/**
 * 带有字符编码的 {@link FileSystemResource} 示例
 * @author xiaodao
 * @version 1.0
 * @date 2021/8/22
 * @see EncodedResource
 */
public class EncodedFileSystemResourceDemo {

    public static void main(String[] args) throws IOException {
        String currentJavaPath = System.getProperty("user.dir") + "" + "/resource/src/main/java/com/tingyuge/thinking/spring/resource/EncodedFileSystemResourceDemo.java";
        File file = new File(currentJavaPath);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        EncodedResource encodedResource = new EncodedResource(fileSystemResource, "utf-8");

        //字符输入流
        Reader reader = encodedResource.getReader();

        System.out.println(IOUtils.toString(reader));
    }
}
